package com.infore.dataReceiving.util;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.infore.dataReceiving.core.chain.Air212InstructionsChain;
import com.infore.dataReceiving.core.enums.Air212InstructionsRegularExpressionEnum;
import com.infore.dataReceiving.core.filter.instructions.air212.RtdFilter;
import com.infore.dataReceiving.dto.AirInstructionsDto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;

public class Air212InstructionsHandleUtil {
	
	public static Map<String, ChannelHandlerContext> cMap = new HashMap<>();
	
	public static Map<String, String> czMap = new HashMap<>();
	
	public static Map<String, String> channelMap = new HashMap<>();
	
//	public static Map<String, Map<String, Object>> dateMap = new HashMap<>();
	
	public static Map<String, String> remoteMap = new HashMap<>();
	
	private static final ThreadLocal<SimpleDateFormat> sdfThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
	
	public static AirInstructionsDto getAirInstructions(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.matches();
		AirInstructionsDto airInstructionsDto = null;
		if(_flag) {
			airInstructionsDto = new AirInstructionsDto();
			airInstructionsDto.setHead("##");
			airInstructionsDto.setDataLength(m.group(1)==null?null:Integer.valueOf(m.group(1)));
			airInstructionsDto.setDatas(m.group(2));
			airInstructionsDto.setQn(m.group(3));
			airInstructionsDto.setSt(m.group(4));
			airInstructionsDto.setCn(m.group(5));
			airInstructionsDto.setPw(m.group(6));
			airInstructionsDto.setMn(m.group(7));
			airInstructionsDto.setDataTime(m.group(8));
			airInstructionsDto.setFactorData(m.group(9));
			airInstructionsDto.setCrc(m.group(10));
			airInstructionsDto.setEnd("\r\n");
		}
		return airInstructionsDto;
	}
	
//	public static AirInstructionsDto getAirInstructions(String msg) {
//		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions.getParrtern()); 
//		Matcher m=p.matcher(msg);
//		boolean _flag = m.matches();
//		AirInstructionsDto airInstructionsDto = null;
//		if(_flag) {
//			airInstructionsDto = new AirInstructionsDto();
//			airInstructionsDto.setHead("##");
//			airInstructionsDto.setDataLength(m.group(1)==null?null:Integer.valueOf(m.group(1)));
//			airInstructionsDto.setDatas(m.group(2));
//			airInstructionsDto.setSt(m.group(3));
//			airInstructionsDto.setCn(m.group(4));
//			airInstructionsDto.setPw(m.group(5));
//			airInstructionsDto.setMn(m.group(6));
//			airInstructionsDto.setDataTime(m.group(7));
//			airInstructionsDto.setFactorData(m.group(8));
//			airInstructionsDto.setCrc(m.group(9));
//			airInstructionsDto.setEnd("\r\n");
//		}
//		return airInstructionsDto;
//	}
	
	public static Map<String, String> getInstructions_Flag(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions_Flag.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		Map<String, String> map = null;
		if(_flag) {
			map = new HashMap<String, String>();
			map.put("factorCode", m.group(1));
			map.put("flag", m.group(2));
		}
		return map;
	}
	
	public static String getInstructions_Rtd(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions_Rtd.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static Map<String, String> getInstructions_ProjectCode(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions_ProjectCode.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		Map<String, String> map = null;
		if(_flag) {
			map = new HashMap<String, String>();
			map.put("factorCode", m.group(1));
			map.put("value", m.group(2));
		}
		return map;
	}
	
	
	public static Timestamp getTimestamp(String dataTime) {
		if(!dataTime.contains("-")) {
			dataTime = dataTime.substring(0,4)+"-"+dataTime.substring(4,6)+"-"+dataTime.substring(6,8)
			+" "+dataTime.substring(8,10)+":"+dataTime.substring(10,12)+":"+dataTime.substring(12,14);
		}
		SimpleDateFormat sdf = sdfThreadLocal.get();
		Date date = null;
		try {
		date = sdf.parse(dataTime);
		} catch (ParseException e) {
		e.printStackTrace();
		}
		return new Timestamp(date.getTime());
	}
	
	public static Air212InstructionsChain getMessageFilterChain() {
		Air212InstructionsChain chain = new Air212InstructionsChain();
		chain.addFilter(new RtdFilter());
		return chain;
	}
	
	public static String getDataLengthString(String dataLength) {
		if(dataLength.length()==1) {
			dataLength="000"+dataLength;
		}else if(dataLength.length()==2) {
			dataLength="00"+dataLength;
		}else if(dataLength.length()==3) {
			dataLength="0"+dataLength;
		}
		return dataLength;
	}
	
	
	/**
	 * 多网卡指定名字类型返回指定InetAddress
	 * 输入类型错误则输出全部网卡接口信息
	 *
	 * @param Name 名字
	 * @param type 类型:  4--Inet4Address  6--Inet6Address
	 * @return
	 */
	public static String getInetAddress(String Name, int type) {
	    try {
	        Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
	        InetAddress addr;
	        if (type == 4) {
	            while (nifs.hasMoreElements()) {
	                NetworkInterface nif = nifs.nextElement();
	                Enumeration<InetAddress> addresses = nif.getInetAddresses();
	                while (addresses.hasMoreElements()) {
	                    addr = addresses.nextElement();
	                    if (nif.getName().equals(Name) && addr instanceof Inet4Address) {
	                        return addr.getHostAddress();
	                    }
	                }
	            }
	        } else if (type == 6) {
	            while (nifs.hasMoreElements()) {
	                NetworkInterface nif = nifs.nextElement();
	                Enumeration<InetAddress> addresses = nif.getInetAddresses();
	                while (addresses.hasMoreElements()) {
	                    addr = addresses.nextElement();
	                    if (nif.getName().equals(Name) && addr instanceof Inet6Address) {
	                        return addr.getHostAddress();
	                    }
	                }
	            }
	        } else {
	            System.out.println("类型指定错误,将输出全部网卡接口信息");
	            System.out.println();
	            while (nifs.hasMoreElements()) {
	                NetworkInterface nif = nifs.nextElement();
	                Enumeration<InetAddress> addresses = nif.getInetAddresses();
	                while (addresses.hasMoreElements()) {
	                    addr = addresses.nextElement();
	                    System.out.println("网卡接口名称：" + nif.getName());
	                    System.out.println("网卡接口地址：" + addr.getHostAddress());
	                    System.out.println();
	                }
	            }
	            return null;
	        }

	    } catch (Exception e) {
	        System.out.println("获取Socket失败");
	    }
	    return null;
	}
	
	
	public static void main(String[] args) throws IOException {
	    String inetAddress =  getInetAddress("eth3", 4);
	    System.out.println(inetAddress);
	}

}
